package com.fangngng.pushall.service.impl;

import com.fangngng.pushall.dto.RequestUserVO;
import com.fangngng.pushall.entity.User;
import com.fangngng.pushall.mapper.UserMapper;
import com.fangngng.pushall.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author fei.fang
 * @date 2021/1/18
 **/
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;


    @Override
    public PageInfo<User> findByUser(RequestUserVO userVO) {
        PageHelper.startPage(userVO.getPageNumber(), userVO.getPageSize());
        PageHelper.orderBy(userVO.getSort());
        User user = new User();
        user.setEmail(userVO.getEmail());
        user.setName(userVO.getUserName());
        user.setStatus(userVO.getStatus());
        user.setCode(userVO.getUserCode());
        List<User> users = userMapper.select(user);
        PageInfo pageInfo = new PageInfo<>(users, userVO.getPageSize());
        return pageInfo;
    }

    @Override
    public User findByEmail(String email) {
        User user = new User();
        user.setEmail(email);
        User userDb = userMapper.selectOne(user);
        if (userDb == null) {
            log.warn("用户:{}不存在", email);
            return null;
        }
        return userDb;
    }

    @Override
    public User addUser(User user) {
        userMapper.insert(user);
        return user;
    }

    @Override
    public User deleteUser(User user) {
        User userDb = userMapper.selectByPrimaryKey(user.getId());
        if (userDb == null) {
            log.warn("用户:{}不存在", user.getEmail());
            return null;
        }
        userDb.setStatus(User.LIFECYCLE_DISABLE);
        userMapper.updateByPrimaryKeySelective(userDb);
        return userDb;
    }

    @Override public User editUser(User user){
        return null;
    }
}
